const path = require('path');
const fileUtils = require('../utils/file_utils');

module.exports = async (ctx, next) => {
    const url = ctx.request.url;
    let filePath = url.replace('/api', '');
    filePath = '../data' + filePath + '.json';
    filePath = path.join(__dirname, filePath);
    try{
        ctx.response.body = await fileUtils.getFileJsonData(filePath);
    }catch  {
        const errorMsg = {
            message: '读取文件内容失败',
            status: 404
        }
        ctx.response.body = JSON.stringify(errorMsg);
    }
    await next();
}